package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    //单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;
    @Autowired
    private ApArticleMapper apArticleMapper;
    /**
     * 根据参数加载文章列表
     * @param homeDto 1为加载更多  2为加载最新
     * @return
     */
    @Override
    public ResponseResult load(Short loadtype,ArticleHomeDto homeDto) {
        //1.参数校验
        Integer size = homeDto.getSize();
        if (size == null||size == 0){
            size = 10;
        }
        size = Math.min(size,MAX_PAGE_SIZE);
        homeDto.setSize(size);

        //文章校验,是否有频道id
        String tag = homeDto.getTag();
        if (StringUtils.isEmpty(tag)){
            homeDto.setTag(ArticleConstants.DEFAULT_TAG);
        }

        //时间校验
        if (homeDto.getMaxBehotTime()==null){ homeDto.setMaxBehotTime(new Date());}
        if (homeDto.getMinBehotTime()==null){ homeDto.setMinBehotTime(new Date());}

        //查询数据
        List<ApArticle> apArticleList = apArticleMapper.loadArticleList(homeDto,loadtype);

        //封装结果
        ResponseResult result = ResponseResult.okResult(apArticleList);

        return result;
    }





}
